﻿Public Class FRestoreFileset
    Private mPassword As String
    Private Sub btnBrowse_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnBrowse.Click
        Try
            With FolderBrowserDialog1
                .SelectedPath = txtRootFolder.Text
                .RootFolder = Environment.SpecialFolder.MyComputer
                .ShowNewFolderButton = False
                .Description = "Select folder with backup set in it"
                Select Case .ShowDialog(Me)
                    Case Windows.Forms.DialogResult.OK
                        txtRootFolder.Text = .SelectedPath
                    Case Else
                        Return
                End Select
            End With

            RefreshFiles()

        Catch ex As Exception
            Framework.Misc.HandleException(Me, ex)
        End Try
    End Sub

    Private Sub RefreshFiles()
        lvwFiles.Items.Clear()

        Dim aFiles As New Generic.List(Of String)
        AddAllBackupFilesRecursive(aFiles, txtRootFolder.Text)

        If aFiles.Count = 0 Then
            MessageBox.Show(Me, "No Backup data files (*.b2bb) where found at the location!", "No data", MessageBoxButtons.OK, MessageBoxIcon.Asterisk)
        End If
        Dim blnAnyValidFile As Boolean = False
        For Each strFilePath As String In aFiles
            If Engine.Manager.IsFileB2BBackupfile(strFilePath) Then
                blnAnyValidFile = True
                Exit For
            End If
        Next

        Dim aItems As New Generic.List(Of ListViewItem)

        For Each strFilePath As String In aFiles
            Dim clsCI As Entities.Backup.BackedUpFileContainerInfo
            Do
                Try
                    clsCI = Engine.Manager.GetBackedUpFileContainerInfo(strFilePath, mPassword)
                    Exit Do
                Catch ex As Engine.WrongPasswordException
                    If mPassword Is Nothing Then
                        mPassword = InputBox("Password needed:", "Encrypted backup file", "")
                    Else
                        mPassword = InputBox("Wrong password! Try again:", "Encrypted backup file", "")
                    End If
                    If mPassword.Trim.Length = 0 Then Exit For
                End Try
            Loop
            Dim clsItem As New ListViewItem
            clsItem.Text = clsCI.FilePath
            clsItem.SubItems.Add(Framework.Misc.ToReadableFileSize(clsCI.FileSize))
            clsItem.SubItems.Add(clsCI.CreationTimeUtc.ToLocalTime.ToString("g"))
            clsItem.SubItems.Add(clsCI.LastWriteTimeUtc.ToLocalTime.ToString("g"))
            clsItem.SubItems.Add(clsCI.Version.ToString)
            clsItem.Tag = clsCI
            aItems.Add(clsItem)
        Next
        lvwFiles.BeginUpdate()
        lvwFiles.Items.Clear()
        lvwFiles.Items.AddRange(aItems.ToArray)
        lvwFiles.EndUpdate()
    End Sub

    Private Sub AddAllBackupFilesRecursive(ByVal list As Generic.List(Of String), ByVal currentPath As String)
        list.AddRange(IO.Directory.GetFiles(currentPath, "*.b2bb"))
        For Each strSubDir As String In IO.Directory.GetDirectories(currentPath)
            AddAllBackupFilesRecursive(list, strSubDir)
        Next
    End Sub

    Private Sub txtTarget_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles txtTarget.TextChanged
        rbCustom.Checked = True
    End Sub

    Private Sub btnBrowseForDestination_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnBrowseForDestination.Click
        With FolderBrowserDialog1
            .SelectedPath = txtRootFolder.Text
            .RootFolder = Environment.SpecialFolder.MyComputer
            .ShowNewFolderButton = True
            .Description = "Select folder to restore files in"
            Select Case .ShowDialog(Me)
                Case Windows.Forms.DialogResult.OK
                    txtTarget.Text = .SelectedPath
                Case Else
                    Return
            End Select
        End With
    End Sub

    Private Sub btnRestore_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnRestore.Click
        If lvwFiles.CheckedItems.Count = 0 Then
            MessageBox.Show(Me, "No files selected!", "Restore", MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
            Return
        End If
        Dim aFilesToRestore As New Generic.List(Of Entities.Backup.BackedUpFileContainerInfo)
        For Each clsItem As ListViewItem In lvwFiles.CheckedItems
            aFilesToRestore.Add(CType(clsItem.Tag, Entities.Backup.BackedUpFileContainerInfo))
        Next
        Dim clsReq As New Engine.RestoreRequest
        clsReq.Files = aFilesToRestore
        If rbCustom.Checked Then
            clsReq.CustomTargetPath = txtTarget.Text
        Else
            clsReq.CustomTargetPath = Nothing
        End If
        clsReq.Password = mPassword
        Dim clsView As New FRestoreFiles
        clsView.Show(Me, clsReq)
        Me.Close()
    End Sub

    Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click
        Me.Close()
    End Sub
End Class